class StringUtil {
	public constructor() {
	}

	public static stringIndexColor: string[] = ["w", "g", "b", "p", "r", "o", "y"];

	/**
	 * 解析字符串中的颜色符号
	 */
	public static parseTextColor(str:string):string{
		let arr = StringUtil.stringIndexColor;
		for(let k in arr){
			let exp = '@' + arr[k];
			let type = str.indexOf(exp);
			if(type != -1){
				str = str.replace(exp, '');
			}
		}
		return StringUtil.getColorString(str, 0xffff00);
	}

	/**
	 * 获取带颜色的字符串
	 */
	public static getColorString(str:string, color:number){
		return '<font color=' + color + '>' + str + '</font>';
	}

	/**
	 * 获取textFlow类型字符串
	 */
	public static getTextFlow(str: string):egret.ITextElement[]{
		return new egret.HtmlTextParser().parser(str);
	}

	/**
	 * 将Json转为HTTP形式的字符串 key=value&key=value..
	 */
	public static jsonToHttpPostString(json:any){
		let str = '';
		for(let k in json){
			str += k + '=' + json[k] + '&';
		}
		str = str.slice(0, str.length-1); 
		return str.trim();
	}

	/**
	 * 将数组转为下划线拼接形式的字符串 1_2_3.
	 */
	public static arrToHttpLineString(arr){
		let str = '';
		for(let v of arr){
			str += v + '_';
		}
		str = str.slice(0, str.length-1); 
		return str.trim();
	}

		/**
	 * 带文本替换功能的字符串
	 * @param StrID
	 * @param valArr
	 * @return 
	 */
	public static getSwfLangStrVar(strID: string, valArr: string[]): string {
		if (DataJson.tips_data == null) {
			return strID;
		}
		var data: any = DataJson.tips_data[strID];
		if (data == null) {
			return strID;
		}

		var strData: string = data.language_content.toString();
		var indexpre: number;
		var indexback: number;
		var strget: string;

		indexpre = strData.indexOf("{");
		indexback = strData.indexOf("}");

		//下一次搜索的起始偏移量,防止{@}嵌套时，造成死循环
		var nextOffset: number = 0;

		var firstIndex: number;

		var strFlagPre: number;
		var strFlagBack: number;
		var strFlag: string;

		let curIndex = 0;

		while (indexpre != -1 && indexback != -1) {
			strget = strData.substring(indexpre, indexback + 1);

			var strreplace: string = valArr[curIndex++].toString();

			strData = strData.replace(strget, strreplace);

			nextOffset = indexpre + strreplace.length;

			indexpre = strData.indexOf("{", nextOffset);
			indexback = strData.indexOf("}", nextOffset);
		}
		return strData;
	}

	/**
	 * 获取配置好的字符串
	 * @param StrID
	 * @return 
	 * 
	 */
	public static getSwfLangStr(strID: string): string {
		if (DataJson.tips_data == null) {
			return strID;
		}
		var data: any = DataJson.tips_data[strID];
		if (data == null) {
			return strID;
		}
		return data.language_content;
	}

	//缩短昵称,超过12个字符，都缩短
    public static getNameSlice(nameStr) {
		if(!nameStr)return;
		let tempName = nameStr;
		if (StringUtil.strlen(nameStr) > 12) {
			tempName = nameStr.substring(0, 6) + "...";
		}
		return tempName;
    }

    //获取字符串长度(英文占1个字符，中文汉字占2个字符)
    public static strlen(str) {
        var len = 0;
        for (var i = 0; i < str.length; i++) {
            var c = str.charCodeAt(i);
            //单字节加1   
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
				len++;
				}
            else {
            	len += 2;
            }
        }
        return len;
    }

}